package com.foreveross.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import com.foreveross.util.DateJsonValueProcessor;
import com.foreveross.util.JSONUtil;
import com.foreveross.util.StringUtil;
import com.foreveross.vo.common.ResultVo;

/**
 * 公共类
 * 
 * @author wm
 * 
 */
public class CommonUtil {
	/**
	 * 返回JSON格式的结果信
	 * 
	 * @param HttpServletResponse
	 *            response
	 * @param resultVo
	 */
	public static void PrintWriterResultVoWithJSON(
			HttpServletResponse response, ResultVo resultVo) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(JSONUtil.toJsonWithGsonBuilder(resultVo));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void PrintWriterObjectWithJSON(
			HttpServletResponse response, Object object) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(JSONUtil.toJsonWithGsonBuilder(object));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public static String getWeixinID(HttpServletRequest request) {
		return request.getParameter("weixinId");
	}
	
	public static String getUserId(HttpServletRequest request) {
		return request.getParameter("userId");
	}

	public static void PrintWriterResultVoWithJSON(
			HttpServletResponse response, List resultVo,final List list) {
		PrintWriter out;
		try {
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			out = response.getWriter();
			JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
			if (list != null && !list.isEmpty()) {
				cfg.setJsonPropertyFilter(new PropertyFilter() {
					public boolean apply(Object source, String name,
							Object value) {
						if (list.contains(name)) {
							return true;
						} else {
							return false;
						}
					}
				});
			}
			JSONArray json = JSONArray.fromObject(resultVo, cfg);
			out.println(json.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 返回JSON格式的结果信
	 * 
	 * @param HttpServletResponse
	 *            response
	 * @param resultVo
	 */
	public static void PrintWriterResultWithJSON(HttpServletResponse response,
			ResultVo resultVo, final List list) {
		PrintWriter out;
		try {
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			out = response.getWriter();
			JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
			if (list != null && !list.isEmpty()) {
				cfg.setJsonPropertyFilter(new PropertyFilter() {
					public boolean apply(Object source, String name,
							Object value) {
						if (list.contains(name)) {
							return true;
						} else {
							return false;
						}
					}
				});
			}
			cfg.setExcludes(new String[] { "handler",
					"hibernateLazyInitializer" });
			JSONObject json = JSONObject.fromObject(resultVo, cfg);
			out.println(json.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void PrintJSONSTR(HttpServletResponse response, String json) {
		PrintWriter out;
		try {
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			out = response.getWriter();
			out.println(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		String uuid = s.substring(0, 8) + s.substring(9, 13)
				+ s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
		System.out.println(uuid + ":" + uuid.length());
		return uuid;
	}

	/**
	 * 获得指定数目的UUID
	 * 
	 * @param number
	 *            int 需要获得的UUID数量
	 * @return String[] UUID数组
	 */
	public static String[] getUUID(int number) {
		if (number < 1) {
			return null;
		}
		String[] ss = new String[number];
		for (int i = 0; i < number; i++) {
			ss[i] = getUUID();
		}
		return ss;
	}

	public static String getHttpBasePath(HttpServletRequest request) {
		return request.getScheme() + "://" + request.getServerName() + ":"
				+ request.getServerPort() + request.getContextPath();
	}
 

	public static String getRealPath(HttpServletRequest request) {
		return request.getSession().getServletContext().getRealPath("");
	}

	public static String getAddrIp(HttpServletRequest request){
		return request.getRemoteAddr();
	}
	
	public static String getRandomString(int length) { //length表示生成字符串的长度
	    String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 }
	/**
	 * 获取富文本里面的链接地址
	 * @param content
	 * @return
	 */
	public static String getContentImgList(String content){
		String contentImgList = null;
		if(null!=content && !"".equals(content)){
			List<String> srcList = StringUtil.getImgSrc(content);
			for(String str : srcList){
				if(contentImgList == null) contentImgList = str;
				else contentImgList = contentImgList +","+ str;
			}
		}
		return contentImgList;
	}
	
	
	
	public static void main(String[] args) {
		String[] ss = getUUID(10);
		for (int i = 0; i < ss.length; i++) {
			System.out.println(ss[i]);
		}
	}

}
